state: {
	// permissionListener: null,
    persimmionList: [
        {
            name: "ACCESS_FINE_LOCATION",
            title: "手机位置权限申请说明:",
            content: "APP正在申请手机地理位置权限，用于推荐距离更近的套餐，允许或拒绝均不会获取任何隐私信息。",
        }, {
            name: "CALL_PHONE",
            title: "拨打电话权限申请说明:",
            content: "APP正在申请拨打电话权限，用于联系商家、客服，允许或拒绝均不会获取任何隐私信息。",
        }, {
            name: "CAMERA",
            title: "读取存储权限申请说明:",
            content: "APP正在申请摄像头、读取相册权限用于扫一扫、更换头像、发布评论、上传图片，允许或拒绝均不会获取任何隐私信息。",
        }, {
            name: "NotificationManagerCompat",
            title: "通知权限申请说明:",
            content: "APP正在申请获取通知权限，用于向您推送订单状态、优惠券状态等通知，允许或拒绝均不会获取任何隐私信息。",
        }, {
            name: "WRITE_EXTERNAL_STORAGE",
            title: "相册写入权限申请说明:",
            content: "APP正在申请获取相册写入权限，用于保存图片至您的相册，允许或拒绝均不会获取任何隐私信息。",
        }
    ],
    dialogView: null
},
mutations: {
	SET_DIALOG_VIEW(state, data) {
        state.dialogView = data;
    },
    SET_PERMISSION_LISTENER(state, data) {
        state.permissionListener = data;
    },
},
actions: {
	//监听授权
	watchPermission({ commit, state, dispatch }){
        commit('SET_PERMISSION_LISTENER', uni.createRequestPermissionListener())
        state.permissionListener.onConfirm((e) => {
            if(e[e.length - 1].toUpperCase().includes('CAMERA') || e[e.length - 1].toUpperCase().includes('READ_MINE') || e[e.length - 1].toUpperCase().includes('PHOTOLIBRARY')){
            	console.log('弹窗提示照片和视频、摄像头、相机授权')
                dispatch('dialogStyle', 2)
            }else if(e[e.length - 1].toUpperCase().includes('LOCATION')){
                console.log('弹窗提示位置授权')
                dispatch('dialogStyle', 0)
            }else if(e[e.length - 1].toUpperCase().includes('CALL_PHONE') || e[e.length - 1].toUpperCase().includes('CONTACT')){
                console.log('弹窗提示拨打电话写入授权')
                dispatch('dialogStyle', 1)
            }else if(e[e.length - 1].toUpperCase().includes('NOTIFI') || e[e.length - 1].toUpperCase().includes('PUSH')){
                console.log('弹窗提示通知推送授权')
                dispatch('dialogStyle', 3)
            }else if(e[e.length - 1].toUpperCase().includes('STORAGE')){
                console.log('弹窗提示相册写入授权')
                dispatch('dialogStyle', 4)
            }
        });
        state.permissionListener.onComplete((e) => {
            dispatch('dialogStyle', -1)
        });
    },
	//停止监听授权
    stopWatchPermission({ commit, state }){
        if(state.permissionListener){
            state.permissionListener.stop();
            commit('SET_PERMISSION_LISTENER', null)
        }
    },
    //创建/关闭弹窗
    //弹窗具体样式可自定义
    dialogStyle({ commit, state }, index = -1) {
        try {
            if (index < 0) return state.dialogView && state.dialogView.close()
            const { title, content } = state.persimmionList[index]
            const statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
            const navigationBarHeight = uni.getSystemInfoSync().platform === 'android' ? 48 :
                44;
            const totalHeight = statusBarHeight + navigationBarHeight; //距离手机顶部的高度
            commit("SET_DIALOG_VIEW", new plus.nativeObj.View('per-modal', {
                top: '0px',
                left: '0px',
                width: '100%',
                backgroundColor: 'rgba(0,0,0,0,0)', // 蒙版背景色
                //opacity: .5;
            }))
            state.dialogView.drawRect({
                color: '#fbe2e2', //弹窗背景色（我用的是浅粉色，白色可能跟页面差距不明显，可以选用app主题色的10%透明）
                radius: '5px'
            }, {
                top: totalHeight + 'px',
                left: '5%',
                width: '90%',
                height: "100px",
            })
            state.dialogView.drawText(title, {
                top: totalHeight + 5 + 'px',
                left: "8%",
                height: "30px"
            }, {
                align: "left",
                color: "#000",
            })
            state.dialogView.drawText(content, {
                top: totalHeight + 35 + 'px',
                height: "60px",
                left: "8%",
                width: "84%"
            }, {
                whiteSpace: 'normal',
                size: "14px",
                align: "left",
                color: "#656563"
            })
            state.dialogView.show()
        } catch (e) {
            console.log(e, '权限说明弹框样式错误');
        }
    },
	code12Fn({ dispatch }, {err, message, title}){
	    if (!err.errMsg.toLowerCase.includes('cancel')){
	    	//取消选择图片不做处理，一般情况下12代表未授权
	        if (err.code == "12") {
	            uni.showModal({
	                title: title,
	                content: message,
	                showCancel: true,
	                confirmText: "去开启",
	                cancelText: "取消",
	                success: (res) => {
	                    if (res.confirm) {
	                        gotoAppPermissionSetting();
	                    }
	                },
	            });
	        } else {
	            uni.showToast({
	                title: title,
	                icon: "error",
	            });
	        }
	    }
	}
}

